/*
 * I write in my free time, for personal using
 * when i fell proud of it, i will give it to public
 * now,have nothing more to say
 */
package com.quangtin3.tunll;

import com.quangtin3.msg.Channel;
import com.quangtin3.utils.TunllConfig;
import com.quangtin3.utils.TunllException;
import com.quangtin3.utils.TunllLogger;
import java.io.IOException;
import java.net.ServerSocket;

/**
 *
 * @author quanghx
 */
public class TunllServer implements Runnable {

    private ServerSocket server;

    public TunllServer() throws TunllException {
        try {
            server = new ServerSocket(TunllConfig.getConfig().getServerPort());
            TunllLogger.sys.info("create server socket listen on port " + TunllConfig.getConfig().getServerPort());
        } catch (IOException e) {
            throw new TunllException(e);
        }
    }

    public void run() {
        try {
            TunllLogger.sys.info("tunll server thread started");
            while (true) {
                Channel channel = new Channel();
                channel.accept(server);
                TunllLogger.sys.debug("accepted connection. " + channel.toString());
                Processor processor = new Processor(channel);
                new Thread(processor).start();
                TunllLogger.sys.debug("create processor thread");
            }
        } catch (TunllException e) {
            TunllLogger.sys.error("exception " + e.getMessage());
        }
    }
}
